<?php
namespace app\sysadmin\controller;
use think\Db;
class Bannerposter extends Common{
	public function __construct(){
		parent::__construct();
	}
	public function index(){
		$_input_data = input('get.');
		$_get_key = array('id','name');
		$_get_str = "";
		
		if($_input_data){
			foreach($_input_data as $k=>$v){
				$_v = trim($v);
				if(in_array($k,$_get_key)){
					if($_v != ''){
						$_get_str .= $k.":'".strval($_v)."',";
					}else{
						$_input_data[$k] = '';
					}
				}else{
					unset($_input_data[$k]);
				}
			}
		}
		if($_get_key){
			foreach($_get_key as $a){
				if(!isset($_input_data[$a])){
					$_input_data[$a] = '';
				}
			}
		}
		$this->assign('get_data',$_input_data);
		$this->assign('get_str',$_get_str);
		
		return $this->fetch();
	}
	public function ajax_lists(){
		if($this->request->isAjax() != true){
			$this->error('404');
		}
		$_input_data = input();
		$page = $_input_data['page'] >= 1 ? (int)$_input_data['page'] : '0';
		$limit = $_input_data['limit'] >= 1 ? (int)$_input_data['limit'] : '15';
		unset($_input_data['page']);
		unset($_input_data['limit']);
		$_where = array();
		if(!empty($_input_data['id'])){
			$_input_data['id'] = (int)$_input_data['id'];
			$_where[] = array('id','=',$_input_data['id']);
		}
		if(!empty($_input_data['name'])){
			$_where[] = array('name','like','%'.$_input_data['name'].'%');
		}
		$count = Db::name('ads_cat')->where($_where)->count();
		$lists = Db::name('ads_cat')->where($_where)->page($page.','.$limit)->field('id,name,info')->order('id asc')->select();
		
		if($lists){
			foreach($lists as $k=>$v){
				$num_all = $this->get_ads_num($v['id'],-1,-1);
				$num_valid_1 = $this->get_ads_num($v['id'],1,1);
				$num_valid_2 = $this->get_ads_num($v['id'],1,2);
				$num_valid_0 = $this->get_ads_num($v['id'],1,0);
				$lists[$k]['num_all'] = $num_all;
				$lists[$k]['num_valid_1'] = $num_valid_1;
				$lists[$k]['num_valid_2'] = $num_valid_2;
				$lists[$k]['num_valid_0'] = $num_valid_0;
				$lists[$k]['num_error'] = $num_all - $num_valid_1 - $num_valid_2 - $num_valid_0;
			}
		}
		$_json = array();
		$_json['code'] = 0;
		$_json['msg'] = 'success';
		$_json['count'] = $count;
		$_json['data'] = $lists;
		echo json_encode($_json);
		exit;
	}
	/**
	 * $cat_id 广告位id 0表示全部
	 * $_status = -1全部 1启用 0禁用
	 * $_valid = -1全部 1有效期内 0过期 2未开始
	**/
	private function get_ads_num($cat_id = 0,$_status = -1,$_valid = -1){
		$_s_name = 'ads_num_'.$cat_id.'_'.$_valid.'_'.$_status;
		$_num = cache($_s_name);
		if($_num){
			return $_num;
		}
		$_where = array();
		if($cat_id > 0){
			$_where[] = array('cat_id','=',$cat_id);
		}
		if($_status == 1){
			$_where[] = array('status','=','1');
		}elseif($_status == 0){
			$_where[] = array('status','=','0');
		}
		$_time = time();
		if($_valid == 1){
			$_where[] = array('start_time','<',$_time);
			$_where[] = array('end_time','>',$_time);
		}elseif($_valid == 0){
			$_where[] = array('end_time','<=',$_time);
		}elseif($_valid == 2){
			$_where[] = array('start_time','>=',$_time);
		}
		$_num = Db::name('ads')->where($_where)->count();
		cache($_s_name,$_num,60);
		return $_num;
	}
}
?>